package com.example.myapplication;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

import java.util.ArrayList;

public class MoreChoiceDialogFragment extends DialogFragment {
    private String[] genders = new String[]{"鸡", "鸭","牛","羊"};
    ArrayList saveChoice;
    private DaChoiceDialogFragment.NotifyDialogListener listener;
    public interface NotifyDialogListener {
        public void onDialogPositiveClicked(String gender);

    }

    public void setListener(DaChoiceDialogFragment.NotifyDialogListener listener) {
        this.listener = listener;
    }

    @Nullable
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        saveChoice=new ArrayList();
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle("多选对话框")
                .setMultiChoiceItems(genders, new boolean[]{true,true,false,false,false}, new DialogInterface.OnMultiChoiceClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                        Toast.makeText(getActivity().getApplicationContext(),"你点击了"+genders[which],Toast.LENGTH_SHORT).show();
                        if (isChecked)
                        {
                            saveChoice.add(genders[which]);
                        }
                        else
                        {
                            saveChoice.remove(genders[which]);
                        }
                    }
                })
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(getActivity().getApplicationContext(),"你确定了！"+saveChoice,Toast.LENGTH_SHORT).show();
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(getActivity().getApplicationContext(),"你取消了！",Toast.LENGTH_SHORT).show();
                    }
                });
        return builder.create();
    }
}
